package com.education.teach.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.widget.TextView;

import com.dn.res.wideget.DnToolbar;
import com.education.teach.R;
import com.education.teach.mvp.contract.WrongCapDeatilContract;
import com.education.teach.mvp.presenter.WrongCapDeatilPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.utils.ArmsUtils;
import com.zzhoujay.richtext.RichText;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import javax.inject.Inject;

import butterknife.BindView;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;

import static com.jess.arms.utils.Preconditions.checkNotNull;

//import com.education.teach.mvp.contract.CoursewareDeatilContract;

//import com.education.teach.mvp.contract.CoursewareDeatilContract;


public class WrongCapDeatilActivity extends BaseActivity<WrongCapDeatilPresenter> implements WrongCapDeatilContract.View {

    @BindView(R.id.toolbar)
    DnToolbar mToolbar;
    @BindView(R.id.tv_my_answer)
    TextView tv_my_answer;
    @BindView(R.id.tv_right_answer)
    TextView tv_right_answer;
    //    @BindView(R.id.iv_question_img)
//    ImageView iv_question_img;
    @BindView(R.id.richText2)
    TextView wv_view;
    @BindView(R.id.richText)
    TextView wv_title;


    @Inject
    IRepositoryManager mIRepositoryManager;
    @Inject
    RxErrorHandler mRxErrorHandler;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
//        DaggerWrongCapDeatilComponent //如找不到该类,请编译一下项目
//                .builder()
//                .appComponent(appComponent)
//                .wrongCapDeatilModule(new WrongCapDeatilModule(this))
//                .build()
//                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_wrongcap_deatil; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    String htmlCode = "<p><img src=\"http://36.48.167.135:8084/study/ueditor1_3_6/jsp/upload/image/20180913/1536802388695028246.png\" title=\"1536802388695028246.png\" alt=\"image.png\"/></p>";
    String htmlTtile = "<p><img src=\"http://36.48.167.135:8084/study/ueditor1_3_6/jsp/upload/image/20180913/1536802367916025370.png\" title=\"1536802388695028246.png\" alt=\"image.png\"/></p>";

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        mToolbar.setMainTitle("错题解析");
        mToolbar.setToolbarLeftBackImageRes(R.mipmap.top_back);
        mToolbar.setOnLeftTitleClickListener(view -> finish());

        Intent intent = getIntent();
        // 原题图片或富文本
        String title = intent.getStringExtra("title");
        // 解析富文本
        String answerKeys = intent.getStringExtra("answerKeys");
        // 我的答案
        String answer = intent.getStringExtra("answer");
        // 正确答案
        String answerQes = intent.getStringExtra("answerQes");

        tv_my_answer.setText("我的答案:     " + answer);
        tv_right_answer.setText("正确答案:      " + answerQes);


        RichText.fromHtml(title)
                .into(wv_title);

        RichText.fromHtml("<p>错题解析：<\\/p><br \\/>"+answerKeys)
                .into(wv_view);

    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    public static String getNewContent(String htmltext) {
        Document doc = Jsoup.parse(htmltext);
        Elements elements = doc.getElementsByTag("img");
        for (Element element : elements) {
            element.attr("width", "100%").attr("height", "auto");
        }
        return doc.toString();
    }

}
